package com.mobile.liuyi_s.orderlunch.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.mobile.liuyi_s.orderlunch.MyApplication;
import com.mobile.liuyi_s.orderlunch.R;
import com.mobile.liuyi_s.orderlunch.Utils.Utils;
import com.mobile.liuyi_s.orderlunch.entity.Order;
import com.mobile.liuyi_s.orderlunch.entity.MyUser;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class SelectOrderNumActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    private Toolbar mToolbar;

    private static final String TAG = "SelectOrderNumActivity";
    private String mSite;
    private int mOrderNum = 1;
    private String corrUser;
    private String mOrderReason;
    private MyApplication mApplication;
    private int mLocalYear;
    private int mLocalMonth;
    private int mLocalDay;

    private RadioGroup mRadioGroupOrderNum;
    private RadioButton mRadioButton1;
    private RadioButton mRadioButton2;
    private RadioButton mRadioButton3;
    private Button mConfirmButton;
    private TextView mSiteTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_num);
        initView();
        //全局变量获取
        mApplication = ((MyApplication) getApplication());
        MyUser user = mApplication.getCurrUser();
        corrUser = user.getUsername();

        Intent intent = getIntent();
        mSite = intent.getStringExtra("site");
        mSiteTextView.setText(mSite);
        mRadioButton1.setChecked(true);
        mRadioGroupOrderNum.setOnCheckedChangeListener(this);
        mConfirmButton.setOnClickListener(this);


        Utils utils = new Utils();
        mLocalYear = utils.getLocacYear();
        mLocalMonth = utils.getLocacMonth();
        mLocalDay = utils.getLocacDay();

    }

    private void initView() {
        mToolbar = ((Toolbar) findViewById(R.id.toolbar_select_num_activity));
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);

        mRadioGroupOrderNum = ((RadioGroup) findViewById(R.id.radio_group_order_num_activity));
        mRadioButton1 = ((RadioButton) findViewById(R.id.radio_btn_1_order_num_activity));
        mRadioButton2 = ((RadioButton) findViewById(R.id.radio_btn_2_order_num_activity));
        mRadioButton3 = ((RadioButton) findViewById(R.id.radio_btn_3_order_num_activity));
        mConfirmButton = ((Button) findViewById(R.id.confirm_btn_order_number_activity));
        mSiteTextView = ((TextView) findViewById(R.id.textview_site_order_num_activity));

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.radio_btn_1_order_num_activity:
                mOrderNum = 1;
                break;
            case R.id.radio_btn_2_order_num_activity:
                mOrderNum = 2;
                break;
            case R.id.radio_btn_3_order_num_activity:
                mOrderNum = 3;
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.confirm_btn_order_number_activity:
//                showDialog();
                if (mOrderNum > 1) {
                    showInputDialog();
                } else {
                    showDialog();
                }
                break;

            default:
                break;
        }
    }

    private void showInputDialog() {
        final EditText orderReason = new EditText(this);
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(this);
        inputDialog.setTitle("请输入份数大于1份的原因！").setView(orderReason);
        inputDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                mOrderReason = orderReason.getText().toString();
                if (mOrderReason.length() > 0) {
                    showDialog();
                } else {
                    Toast.makeText(SelectOrderNumActivity.this, "请输入份数大于两份原因！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        inputDialog.show();
    }
    private void showDialog() {
        String dialogContent = "站点：zhy" + mSite + ", 份数：" + mOrderNum;
        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("订单确认");
        dialog.setMessage(dialogContent);
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
//                if (!mApplication.getIsOrdered()) {
                    if (corrUser != null && mSite.length() > 0 && mOrderNum > 0) {
                        if (corrUser.length() != 0){
                            Order order = new Order();
                            order.setOrderUser(corrUser);
                            order.setOrderSite(mSite);
                            order.setOrderNum(mOrderNum);
                            order.setOrderYear(mLocalYear);
                            order.setOrderMonth(mLocalMonth);
                            order.setOrderDay(mLocalDay);
                            order.setOrderReason(mOrderReason);
                            finish();
                            order.save(new SaveListener<String>() {
                                @Override
                                public void done(String objectId, BmobException e) {
                                    if (e == null) {
                                        mApplication.setOrdered(true);
                                        Toast.makeText(getApplicationContext(), "订单提交成功", Toast.LENGTH_SHORT).show();
//                                        finish();
                                    } else {
                                        Toast.makeText(getApplicationContext(), "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
//                    }
                } else {
                    Toast.makeText(getApplicationContext(), "已经订餐", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });
        dialog.setNegativeButton("关闭", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                Toast.makeText(SelectOrderNumActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();
    }
}
